package com.system.connect.springboot_flowers.controller;

import cn.hutool.crypto.digest.MD5;
import com.system.connect.springboot_flowers.entity.Admins;
import com.system.connect.springboot_flowers.dto.UsersDto;
import com.system.connect.springboot_flowers.service.IAdminsService;
import com.system.connect.springboot_flowers.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author yang
 * @since 2025-01-13
 */
@RestController
@RequestMapping("/api/admins")
public class AdminsController {

    @Autowired
    private IAdminsService adminsService;

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public R register(@RequestBody UsersDto usersDto) {
        adminsService.register(usersDto);
        return R.success("注册成功");
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public R login(@RequestBody UsersDto usersDto) {
        String password = MD5.create().digestHex(usersDto.getPassword());
        Admins user = adminsService.lambdaQuery()
                .eq(Admins::getUsername, usersDto.getUsername())
                .eq(Admins::getPasswordHash, password)
                .one();
        if (user != null) {
            return R.success("登录成功");
        } else {
            return R.error("用户名或密码错误");
        }
    }

}
